package com.ccqlk.lightpointrelation;

import cn.idev.excel.FastExcel;
import com.ccqlk.lightpointrelation.util.GPSUtil;
import org.apache.commons.lang3.StringUtils;


import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

public class GeoJson转换 {
    private static final String AROUND_JSON = """
                    {
                        "type": "FeatureCollection",
                        "features": [%s]
                    }
                """;
    private static final String CYCLE_JSON= """
                    {
                        "geometry": {
                            "type": "Point",
                            "coordinates": [%s, %s]
                        },
                        "type": "Feature",
                        "properties": {
                            "id": "%s",
                            "箱变名称": "%s",
                            "X坐标": "%s",
                            "Y坐标": "%s",
                            "经纬度": "%s",
                            "控制器编号": "%s",
                            "箱变出线回路": "%s",
                            "二级配电柜名称": "%s",
                            "回路数": "%s",
                            "路段名称": "%s",
                            "灯杆数量": "%s",
                            "备注": "%s"
                        }
                    }
                """;

    public static void main(String[] args) throws IOException {
        String readUrl = "E:\\cqj\\箱变管线管点\\沙区踩点信息表-2-normal.xlsx";
        String writeUrl1 = "E:\\cqj\\箱变管线管点\\沙区踩点信息表-2-normal.json";


        List<Object> objects1 = FastExcel
                .read(readUrl)
                .sheet(0)
                .doReadSync();

        List<Object> objects2 = FastExcel
                .read(readUrl)
                .sheet(1)
                .doReadSync();

        objects1.addAll(objects2);

        // 写json
        String json1 = getJson(objects1);
        BufferedWriter bufferedWriter1 = new BufferedWriter(new FileWriter(writeUrl1));
        bufferedWriter1.write(json1);
        bufferedWriter1.flush();
    }


    private static String getJson(List<Object> objects){
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < objects.size(); i++) {
            if(i > 0){
                sb.append(",");
            }
            LinkedHashMap<Integer,String> mapObj = (LinkedHashMap<Integer,String>)objects.get(i);
            // 必须有xy信息
            String xyInfo = mapObj.get(4);
            if(StringUtils.isBlank(xyInfo)){
                continue;
            }
            String[] split = mapObj.get(4).split("\\n");
            double[] doubles = GPSUtil.gps84ToGcj02(Double.parseDouble(split[1]), Double.parseDouble(split[0]));
            // 处理所有字段\n
            dealSafeStr(mapObj);


            double x = doubles[1];
            double y = doubles[0];
            String formatted = CYCLE_JSON.formatted(x, y, mapObj.get(0), mapObj.get(1), mapObj.get(2), mapObj.get(3), mapObj.get(4), mapObj.get(5),
                    mapObj.get(6), mapObj.get(7), mapObj.get(8), mapObj.get(9), mapObj.get(10), mapObj.get(11));
            sb.append(formatted);
        }
        return AROUND_JSON.formatted(sb.toString());
    }

    private static void dealSafeStr(LinkedHashMap<Integer,String> mapObj){
        // 避免 ConcurrentModificationException
        List<Integer> keys = new ArrayList<>(mapObj.keySet());
        for (Integer key : keys) {
            String value = mapObj.get(key);
            if(StringUtils.isNotBlank(value)){
                String replace = value.replace("\n", "<===>");
                mapObj.put(key, replace);
            }
        }
    }

}
